* Replication Code: Did the Budapest Convention Deter Cybercrime? An Event Study Using a Panel of Countries
* Authors: Humza Khan and Pengfei Zhang
* Data: ITU employees

* Install programs
ssc install reghdfe
ssc install event_plot
ssc install did_imputation
ssc install csdid
ssc install drdid
ssc install ivreg2
ssc install avar
ssc install eventstudyinteract
ssc install did_multiplegt

* Import data 

** converts strings to numeric
destring, replace

**reshape dataset
*unique identifier for each country
gen country_num = _n 

*Reshape data from wide to long, including controls 
reshape long employees, i(country_num) j(year)

*
tsset country_num year  

*Create treatment variable: timeToTreat 
gen timeToTreat = year - Eventtime 
gen treatment = cond(year >= Eventtime,1,0)

* preserve for analysis 
preserve

 
*drop observations based on time window
drop if timeToTreat < -17 
drop if timeToTreat > 17

*bc event windows. 
forvalues l = 0/17 {  
		gen L`l'event = timeToTreat == `l'
	} 
forvalues l = 1/16 { 
		gen F`l'event = timeToTreat ==-`l' 
} 
drop F1event   

*Dynamic Regression  
reghdfe employees F*event L*event, absorb(country_num year) cluster(country_num)
estimates store ols  
 

*** Graph: DiD Estimator Comparisons ***
 
*** Callaway and San'Anna *** SHOWS ALL ZERO VALUES
*ssc install csdid
*ssc install drdid

gen gvar = cond(Eventtime==., 0, Eventtime)  
csdid employees, ivar(country_num) time(year) gvar(gvar) notyet event
estat event, estore(cs)  

*** Sun and Abraham ***
*ssc install ivreg2
*ssc install avar

gen lastcohort = Eventtime==r(max)
eventstudyinteract employees L*event F*event, vce(cluster country_num) absorb(country_num year) cohort(Eventtime) control_cohort(lastcohort)
matrix sa_b = e(b_iw)  
matrix sa_v = e(V_iw)
 
*** de Chaisemartin-D'Haultfoeuille ***

*ssc install did_multiplegt

did_multiplegt_old employees country_num year treatment, robust_dynamic dynamic(12) placebo(4) breps(100) cluster(country_num) 
matrix dcdh_b = e(estimates)  
matrix dcdh_v = e(variances)


*HTE Estimator Graph

event_plot dcdh_b#dcdh_v cs sa_b#sa_v ols, ///
	stub_lag(Effect_# Tp# L#event L#event) stub_lead(Placebo_# Tm# F#event F#event) plottype(scatter) ciplottype(rcap) ///
	together perturb(-0.325(0.13)0.325) trimlead(4) trimlag(7) noautolegend ///
	graph_opt( ///
		xtitle("{bf: Years Before and After Ratification}",size(small)) ytitle("{bf: Estimated Change in Full-time IT Employees}",size(small)) xlabel(-4(1)7, labsize(vsmall)) ylabel(-25000(25000)25000, labsize(vsmall)) ///
		legend(order(2 "de Chaisemartin-D'Haultfoeuille" ///
				4 "Callaway-Sant'Anna" 6 "Sun-Abraham" 8 "OLS") rows(2) position(6) region(lcolor(black) lpattern(longdash)) size(small) placement(center) lalign(center)) ///
		xline(-1, lcolor(gs12) lpattern(dash)) yline(0, lpattern(dash) lcolor(gs12)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal) labsize(vsmall)) ///
	) ///
	lag_opt1(msymbol(O) color(orange)) lag_ci_opt1(color(orange)) ///
	lag_opt2(msymbol(Dh) color(blue)) lag_ci_opt2(color(blue)) ///
	lag_opt3(msymbol(Th) color(emerald)) lag_ci_opt3(color(emerald)) ///
	lag_opt4(msymbol(Sh) color(black)) lag_ci_opt4(color(black)) 
 



 
